layui.use(['table', 'jquery', 'layer', 'flowers'], function () {

    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery,
        flowers = layui.flowers;

    var tableIns = table.render({
        elem: '#generatorTable',
        url: flowers.ctxPath + '/generator/generator/list',
        method: "post",
        page: true,
        cellMinWidth: 100,
        cols: [[
            {type: 'checkbox'},
            {field: 'tableName', title: '表名'},
            {field: 'dataRows', title: '数据行', sort: true},
            {field: 'tableDescribe', title: '表描述'},
            {field: 'createTime', title: '创建时间', sort: true},
            {field: 'updateTime', title: '修改时间', sort: true},
            {fixed: 'right', align: 'center', toolbar: '#generatorBar', minWidth: 300, title: '操作'}
        ]]
    });

    // 搜索
    $('#searchBtn').on('click', function () {
        tableIns.reload({
            where: flowers.getFormData('generatorForm'),
            page: {curr: 1}
        });
    });

    // 重置搜索条件
    $('#resetBtn').on('click', function () {
        document.getElementById('generatorForm').reset();
        tableIns.reload({
            where: flowers.getFormData('generatorForm'),
            page: {curr: 1}
        });
    });

    // 生成代码
    function execute(data) {
        layer.open({
            type: 2,
            title: '生成代码',
            content: flowers.ctxPath + '/view/generator/add?tableName=' + data.tableName,
            area: ['80%', '80%'],
            max: true
        });
    }

    // 表格行工具栏事件监听
    table.on('tool(generatorTable)', function (obj) {
        var data = obj.data,
            event = obj.event;
        if (event === 'generate') {
            execute(data);
        }
    });

});